﻿using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class TAV12
    {


        public TAV12()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VABFOLGE();
            this.DataContext = MV;
            InitializeComponent(); 
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            if (LFD == "000000")
            {
                LFD = "";
            }
            dwData = TBMain.NewRow();
            dwData["ABFMANDANT"] = app.Mandant;
            dwData["ABFAUFKRS"] = " AND ABFAUFKRS LIKE'" + KRS.Trim() + "%'";
            dwData["ABFAUFLFD"] = " AND ABFAUFLFD LIKE'" + LFD.Trim() + "%'";
            dwData["ABFKSTKRS"] = KSTKRS;
            dwData["ABFKST"] = KST;
            dwData["ABFSTATUS"] = " ";
            dwData["ABFAGANG"] = AGANG;
            dwData["ABFMTYP"] = MTYP;
            dwData["ABFISTARTDAT"] = "19900101";
            SResult = TMV.QueryABFOLGE(dwData, 9);
            MergeData(SResult, false);
            //base.QueryCommand(sender, e);
            using (new LongOperation(this, "开始同步"))
            {
                RESPLIT();
            }
        }


        public void RESPLIT()
        {
            if (!FTAUTOSPLIT)
                return;
            string _ABF;
            MessageInfo msg = new MessageInfo();
            int count = TBMain.Rows.Count;
            int idx = 0;
            EventDispatcher.Instance.RaiseStatus(MV.INFO, StatusEventType.StartProgress);
            foreach (DataRow row in TBMain.Rows)
            {
                idx++;
                _ABF = row["ABFAUFKRS"].ToString() + row["ABFAUFLFD"].ToString() + row["ABFAUFPOS"].ToString() + row["ABFAFOLGE"].ToString();
                TMV.KSL.LOADABF(_ABF);
                TMV.MK.LOADABF(_ABF);
                msg.MSGTYPE = "RESPLIT";
                msg.ACTION = "RESPLIT";
                msg.KRS = row["ABFAUFKRS"].ToString();
                msg.LFD = row["ABFAUFLFD"].ToString();
                msg.POS = row["ABFAUFPOS"].ToString();
                msg.P1 = row["ABFAFOLGE"].ToString();
                msg.P2 = _ABF;
                DataSet Set = new DataSet();
                Set.Merge(TMV.KSL.TBMain);
                msg.Set = Set;
                TMV.MK.RESPLIT(msg);
                MV.INFO = _ABF + ":" + idx + "//" + count;
                EventDispatcher.Instance.RaiseStatus(MV.INFO, StatusEventType.RunningProgress);
            }
            //  MK.POSTCHG();
            EventDispatcher.Instance.RaiseStatus(MV.INFO, StatusEventType.StopProgress);
        }

    }
}
